# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

require 'zlib'
require 'stringio'

class ApplicationController < ActionController::Base
  include AuthenticatedSystem
  
  helper :all

  before_filter :login_from_cookie
  before_filter :login_required, :only => [ :new, :create, :edit, :update, :destroy ]
  after_filter :compress

  layout 'default'

  Localization.lang = 'en_US'

  protected
  def compress
    accepts = request.env['HTTP_ACCEPT_ENCODING' ]
    return unless accepts && accepts =~ /(x-gzip|gzip)/
    encoding = $1
    output = StringIO.new
    def output.close # Zlib does a close. Bad Zlib...
      rewind
    end
    gz = Zlib::GzipWriter.new(output)
    gz.write(response.body)
    gz.close
    if output.length < response.body.length
      response.body = output.string
      response.headers['Content-encoding' ] = encoding
    end
  end
  
  def redirect_back_or(path = home_path)
    if request.env['HTTP_REFERER']
      redirect_to :back
    else
      redirect_to path
    end
  end
end
